package com.juyu.reader.author.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.juyu.reader.author.model.Author;
import com.juyu.reader.author.util.StringUtil;

public class AuthorInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		// TODO Auto-generated method stub
		Author author = (Author) request.getSession().getAttribute(Configure.systemSessionAuthor);
		if (author != null) {
			// 设置当前菜单选择
			int pMenuId = 0;
			int cMenuId = 0;
			String pMenuIdStr = request.getParameter("pMenuId");
			String cMenuIdStr = request.getParameter("cMenuId");
			if (!StringUtil.isNullOrEmpty(pMenuIdStr)) {
				try {
					pMenuId = Integer.valueOf(pMenuIdStr);
					request.getSession().setAttribute("pMenuId", pMenuId);
				} catch (Exception e) {
				}
			}
			if (!StringUtil.isNullOrEmpty(cMenuIdStr)) {
				try {
					cMenuId = Integer.valueOf(cMenuIdStr);
					request.getSession().setAttribute("cMenuId", cMenuId);
				} catch (Exception e) {
				}
			}

			return true;
		}
		request.getRequestDispatcher("/author/toLogin").forward(request, response);
		return false;
	}

}
